package h0826;

import java.util.Arrays;

public class QuickSort {
    public static void main(String[] args) {
        int[] nums=new int[]{2,3,1,5,4};
        quickSort(nums,0,4);
        Arrays.stream(nums).forEach(System.out::println);
    }
    private static void quickSort(int[] nums,int low,int high){
        if (low<high){
            int pi=partition(nums,low,high);
            quickSort(nums,low,pi-1);
            quickSort(nums,pi+1,high);
        }
    }

    private static int partition(int[] nums, int low, int high) {
        int pivot=nums[low];
        int temp=low;
        while (low<high&&low<nums.length&&high< nums.length){
            while (low< nums.length&&nums[low]<=pivot){
                low++;
            }

            while (high>=0&&nums[high]>pivot){
                high--;
            }
            if (low<high) {
                swap(nums, low, high);
                low++;
                high--;
            }
        }
        if (low>high){
            swap(nums,high,temp);
        }
        return high;
    }

    private static void swap(int[] nums, int low, int high) {
        int temp=nums[low];
        nums[low]=nums[high];
        nums[high]=temp;
    }
}
